package javaConnection;

import java.util.LinkedList;

import twitter4j.Query;
import twitter4j.QueryResult;
import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.conf.ConfigurationBuilder;

public class SearchTwitter {
	
	private static SearchTwitter _instance;
	
	private Twitter twitter;
	
	private SearchTwitter(){
		 ConfigurationBuilder cb = new ConfigurationBuilder();
		    cb.setDebugEnabled(true);
		    cb.setOAuthConsumerKey("8jp2OTwr01DhvvULzUalg");
		    cb.setOAuthConsumerSecret("IkFCaLQqXZxiVvuxup7ryMg5hGks1qfRICkXD1DJLQ");
		    cb.setOAuthAccessToken("490808749-AeQXBK9dnEkDNAZu0IRlaDJSMvaUxS3pJ1B4NTBI");
		    cb.setOAuthAccessTokenSecret("jSVIRBzp1ztyf3ChffhTdijCAao8SK1ywzzi1NAuMo");
		    TwitterFactory tf = new TwitterFactory(cb.build());
		    twitter = tf.getInstance();
	}
	
	public static SearchTwitter getInstance(){
	
		if (_instance == null)
			_instance = new SearchTwitter();
		
		return _instance;
		
	}
	
	
	

	public LinkedList<String> search(String word){
		
		LinkedList<String> ret = new LinkedList<String>();
		
	    Query query = new Query(word);
	    query.setCount(100);
	    QueryResult result = null;
	    	    

		try {
			result = twitter.search(query);
		} catch (TwitterException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	    for (Status status : result.getTweets()) {
	        System.out.println("@" + status.getUser().getScreenName() + ":" + status.getText());
	    	ret.add("@" + status.getUser().getScreenName() + ":" + status.getText());
	    }
	    
	    return ret;
	    
	}

}
